/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
/*
class Solution {
public:
    TreeNode* reConstructBinaryTree(vector<int> pre,vector<int> vin) {
        return recur(pre, vin, 0, pre.size()-1, 0, vin.size()-1);
    }
    TreeNode* recur(vector<int>& pre, vector<int>& in, int prel, int prer, int inl, int inr) {
        // if(prel > prer) return nullptr;
        if(inl > inr) return nullptr;
        int node_val = pre[prel];
        int index = inl;
         // while(in[index]!=node_val) {index++;}
        for(index= inl; index<=inr;index++) {
            if(in[index]==node_val) break;
        }
        int l_num = index - inl;
        TreeNode* node = new TreeNode(node_val);
        node->left=recur(pre, in, prel+1, prel+l_num, inl, index-1);
        node->right=recur(pre, in ,prel+l_num+1, prer, index+1, inr);
        return node;
    }
    
};
*/
class Solution {
public:
    TreeNode *reConstructBinaryTree(vector<int> pre,vector<int> vin) {
        if (pre.empty())
            return nullptr;
        TreeNode *root = new TreeNode(pre.front());
        vector<int>::iterator root_vin = find(vin.begin(), vin.end(), root->val);
        int rootIdx_vin = root_vin - vin.begin();
        root->left = reConstructBinaryTree({pre.begin() + 1, pre.begin() + rootIdx_vin + 1}, {vin.begin(), root_vin});
        root->right = reConstructBinaryTree({pre.begin() + rootIdx_vin + 1, pre.end()}, {root_vin + 1, vin.end()});
        
        return root;
    }
};